package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.common.constant.Constants;
import cn.itsource.ymcc.common.jwt.JwtUtils;
import cn.itsource.ymcc.common.result.JsonResult;
import cn.itsource.ymcc.common.result.PageList;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.domain.CourseOrderItem;
import cn.itsource.ymcc.dto.KillPlaceOrderDto;
import cn.itsource.ymcc.dto.PlaceOrderDto;
import cn.itsource.ymcc.query.CourseOrderQuery;
import cn.itsource.ymcc.service.ICourseOrderItemService;
import cn.itsource.ymcc.service.ICourseOrderService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    public ICourseOrderService courseOrderService;
    @Autowired
    private ICourseOrderItemService courseOrderItemService;

    @GetMapping("/getUserIdAndCourseId/{orderNo}")
    public JsonResult getUserIdAndCourseId(@PathVariable("orderNo")String orderNo){
        CourseOrder one = courseOrderService.getOne(new LambdaQueryWrapper<CourseOrder>()
                .eq(CourseOrder::getOrderNo, orderNo));
        List<CourseOrderItem> list = courseOrderItemService.list(new LambdaQueryWrapper<CourseOrderItem>()
                .eq(CourseOrderItem::getOrderNo, orderNo));
        List<Long> courseId = list.stream().map(CourseOrderItem::getCourseId).collect(Collectors.toList());
        Long userId = one.getUserId();
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("courseId",courseId);
        return JsonResult.success().setData(JSON.toJSONString(map));
    }

    @PostMapping("/placeOrder")
    public JsonResult placeOrder(@RequestBody PlaceOrderDto orderDto, HttpServletRequest request){
        String header = request.getHeader(Constants.TOKEN);
        //获取当前登录人
        JSONObject currentUser = (JSONObject)JwtUtils.getCurrentUser(header);
        Long loginId = ((Integer)currentUser.get("loginId")).longValue();
        return courseOrderService.placeOrder(orderDto,loginId);
    }

    @PostMapping("/killPlaceOrder")
    public JsonResult killPlaceOrder(@RequestBody KillPlaceOrderDto orderDto, HttpServletRequest request){
        String header = request.getHeader(Constants.TOKEN);
        //获取当前登录人
        JSONObject currentUser = (JSONObject)JwtUtils.getCurrentUser(header);
        Long loginId = ((Integer)currentUser.get("loginId")).longValue();
        return courseOrderService.killPlaceOrder(orderDto,loginId);
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody CourseOrder courseOrder){
        if(courseOrder.getId()!=null){
            courseOrderService.updateById(courseOrder);
        }else{
            courseOrderService.save(courseOrder);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseOrderService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(courseOrderService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(courseOrderService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseOrderQuery query){
        Page<CourseOrder> page = new Page<CourseOrder>(query.getPageNo(), query.getPageSize());
        page = courseOrderService.page(page);
        return JsonResult.success().setData(new PageList<CourseOrder>(page.getTotal(), page.getRecords()));
    }
}
